package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.POSProdTemplate;
import Models.POSSetup;

public class POSSetupPresenter extends AbstractPresenter<POSSetup> {
	final String SINGLEITEM = "posSetup";
	final String MULTIITEM = "pOSSetups";
	
	public ArrayList<POSSetup> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSSetup tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public POSSetup tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			POSSetup setup = createItemFromJSONObject(jsonHop);
			return setup;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSSetup createItemFromJSONObject(JSONObject jsonSetup){
		POSSetup setup = new POSSetup();
		try{
			setup.setId(jsonSetup.getInt("id"));
			setup.setTasterDiscount(jsonSetup.getDouble("tasterDiscount"));
			setup.setBirthdayBeer(jsonSetup.getBoolean("birthdayBeer"));
			setup.setGrowlerCount(jsonSetup.getInt("growlerCount"));
			setup.setLoyaltyProgram(jsonSetup.getBoolean("loyaltyProgram"));
			setup.setValuePerPoint(jsonSetup.getDouble("valuePerPoint"));
			setup.setGrowlerProgram(jsonSetup.getBoolean("growlerProgram"));
			if(!jsonSetup.isNull("glassware")){
				//create product if set
				POSGlassWarePresenter glasswarePresenter = new POSGlassWarePresenter();
				JSONObject jsonGlassware = jsonSetup.getJSONObject("glassware");
				setup.setGlassware(glasswarePresenter.createItemFromJSONObject(jsonGlassware));
			} else {
				setup.setGlassware(null);
			}
			if(!jsonSetup.isNull("growlerDiscount")){
				//create product if set
				POSDiscountPresenter discountPresenter = new POSDiscountPresenter();
				JSONObject jsonDiscount = jsonSetup.getJSONObject("growlerDiscount");
				setup.setGrowlerDiscount(discountPresenter.createItemFromJSONObject(jsonDiscount));
			} else {
				setup.setGrowlerDiscount(null);
			}
			//Set POSProductServingTypeCollection.
			ArrayList<POSProdTemplate> psts = new ArrayList<POSProdTemplate>();
			if(!jsonSetup.isNull("productTemplate")){
				Object item = jsonSetup.get("productTemplate");
				POSProdTemplatePresenter pstPresenter = new POSProdTemplatePresenter();
				if(item instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)item;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonPST = jPSTArray.getJSONObject(i);
						psts.add(pstPresenter.createItemFromJSONObject(jsonPST));
					}
				} else {
					JSONObject jsonPST = (JSONObject)item;
					psts.add(pstPresenter.createItemFromJSONObject(jsonPST));
				}
				setup.setProductTemplate(psts);
			}
			return setup;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSSetup item){
		try{
			JSONObject json = new JSONObject();
			json.put("id", item.getId());
			json.put("tasterDiscount", item.getTasterDiscount());
			json.put("birthdayBeer", item.isBirthdayBeer());
			json.put("growlerCount", item.getGrowlerCount());
			json.put("growlerProgram", item.isGrowlerProgram());
			json.put("loyaltyProgram", item.isLoyaltyProgram());
			json.put("valuePerPoint", item.getValuePerPoint());
			if(item.getGlassware()!=null){
				final POSGlassWarePresenter glasswarePresenter = new POSGlassWarePresenter();
				json.put("glassware", glasswarePresenter.createJSON(item.getGlassware()));
			} else {
				json.put("glassware", JSONObject.NULL);
			}
			if(item.getGrowlerDiscount()!=null){
				final POSDiscountPresenter discountPresenter = new POSDiscountPresenter();
				json.put("growlerDiscount", discountPresenter.createJSON(item.getGrowlerDiscount()));
			} else {
				json.put("growlerDiscount", JSONObject.NULL);
			}
			if(item.getProductTemplate()!=null){
				POSProdTemplatePresenter pstPresenter = new POSProdTemplatePresenter();
				if(item.getProductTemplate().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getProductTemplate().size(); i++){
						jsonArray.put(pstPresenter.createJSON(item.getProductTemplate().get(i)));
					}
					json.put("productTemplate", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSSetup getEmptyEntity(){
		POSSetup entity = new POSSetup();
		return entity;
	}
}
